::: scene subject

# GORM 框架篇

## 限定字段和排序

:::

::: scene 限定字段-函数参数

## 代码实现

```go
db.Select("name", "age").Find(&users)
```

## 构造语句

```sql
SELECT name, age FROM users
```

:::

::: scene 限定字段-字符串数组

## 代码实现

```go
db.Select([]string{"name", "age"}).Find(&users)
```

## 构造语句

```sql
SELECT name, age FROM users
```

:::

::: scene 限定字段-预处理语句

## 代码实现

```go
db.Table("users").Select("COALESCE(age,?)", 42).Rows()
```

## 构造语句

```sql
SELECT COALESCE(age,'42') FROM users
```

:::

::: scene 排序-字符串

## 代码实现

```go
db.Order("age desc, name").Find(&users)
```

## 构造语句

```sql
SELECT * FROM users ORDER BY age desc, name
```

:::

::: scene 排序-多函数多排序

## 代码实现

```go
db.Order("age desc").Order("name").Find(&users)
```

## 构造语句

```sql
SELECT * FROM users ORDER BY age desc, name
```

:::

::: scene 排序-表达式

## 代码实现

```go
db.Clauses(clause.OrderBy{
  Expression: clause.Expr{SQL: "FIELD(id,?)", Vars: []interface{}{[]int{1, 2, 3}}, WithoutParentheses: true},
}).Find(&User{})
```

## 构造语句

```sql
SELECT * FROM users ORDER BY FIELD(id,1,2,3)
```

:::

::: scene 感谢观看

<p className="text-center text-4xl mt-32">如果你喜欢我创作的内容，邀请你关注、投币或收藏</p>
<p className="text-center text-4xl mt-24" style={{ color: "#ffde00" }}>Author：黑爷</p>

:::



